package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyStatisVORedisService strategyRedisService;


    @Override
    public void insert(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public Strategy findById(String id) {
        Optional<Strategy> strategy = strategyRepository.findById(id);
        return strategy.orElse(null);
    }

    @Override
    public List<Strategy> findAll() {
        List<Strategy> all = strategyRepository.findAll();
        return all;
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();

        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        if (StringUtils.hasText(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        long total = template.count(query, Strategy.class);
        if (total == 0){
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Strategy> list = template.find(query, Strategy.class);
        return new PageImpl<>(list, pageable,total);
    }

    @Override
    public List<Strategy> findByDestName(String destName) {
        List<Strategy> strategies = strategyRepository.findByDestName(destName);
        return strategies;
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        StrategyCatalog strategyCatalog = strategyCatalogService.findById(strategy.getCatalogId());
        //destId
        strategy.setDestId(strategyCatalog.getDestId());
        //destName
        strategy.setDestName(strategyCatalog.getDestName());
        //themeName
        strategy.setThemeName(strategyThemeService.findById(strategy.getThemeId()).getName());
        //catalogName
        strategy.setCatalogName(strategyCatalog.getName());
        //国外还是国内
        List list = destinationService.queryToasts(strategy.getDestId());
        strategy.setIsabroad("中国".equals(list.get(0)) ? 0:1);

        if (StringUtils.hasLength(strategy.getId())){
            //编辑要先查询
            Strategy strategy1 = this.findById(strategy.getId());
            //更新,需要解决冗余字段
            strategy.setViewnum(strategy1.getViewnum());
            strategy.setReplynum(strategy1.getReplynum());
            strategy.setFavornum(strategy1.getFavornum());
            strategy.setSharenum(strategy1.getSharenum());
            strategy.setThumbsupnum(strategy1.getThumbsupnum());
            strategy.setCreateTime(strategy1.getCreateTime());

            this.update(strategy);
        }else {
            //创建时间
            strategy.setCreateTime(new Date());
            this.insert(strategy);
        }
    }

    @Override
    public void updateState(int state, String id) {
        Query query = new Query();
        Update update = new Update();
        update.set("state", state);
        query.addCriteria(Criteria.where("_id").is(id));

        template.updateMulti(query, update, Strategy.class);

    }

    @Override
    public List<Strategy> findByCatalogId(String catalogId) {
        List<Strategy> strategies = strategyRepository.findByCatalogId(catalogId);
        return strategies;
    }

    @Override
    public List<Strategy> findByDestIdTon3(String destId) {
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy> strategies = strategyRepository.findByDestId(destId,pageable);
        return strategies;
    }


}
